package com.example.scanningshardutils.utils;

import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;


import com.example.scanningshardutils.R;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class UtilsShardDialog extends Dialog {

    FragmentActivity context;
    Drawable logo;
    String url;

    public static void getInstance(FragmentActivity context, Drawable drawable, String url) {
        new UtilsShardDialog(context, drawable, url).show();
    }

    public UtilsShardDialog(FragmentActivity context, Drawable drawable, String url) {
        super(context, R.style.utils_dialog);
        this.context = context;
        this.logo = drawable;
        this.url = url;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.dialog_utils_search_layout, null);
        setContentView(view);

        final ImageView img = findViewById(R.id.img);
        final Bitmap[] bitmap1 = {null};
        new Thread(new Runnable() {
            @Override
            public void run() {
                final Bitmap mLogo = QrCodeUtil.createQRCode(url, FileUtilsJ.gitBitmap(logo));
                Bitmap bitmap = FileUtilsJ.convertViewToBitmap(img);
                bitmap1[0] = FileUtilsJ.combineBitmap(bitmap, mLogo);
            }
        }).start();

        findViewById(R.id.tv_shard).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null == bitmap1[0]) return;
                File shard = FileUtilsJ.saveImageToGallery(bitmap1[0], "shard");
                String imageUri = FileUtilsJ.insertImageToSystem(context, shard.getPath());
                Intent shareIntent = new Intent();
                shareIntent.setAction(Intent.ACTION_SEND);
                shareIntent.putExtra(Intent.EXTRA_TEXT, "一些文字");
                shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(imageUri));
                shareIntent.setType("image/*");
                context.startActivity(shareIntent);//这样分享就不带标题了
                context.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        dismiss();
                    }
                });

            }
        });
        findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }


    public void show() {
        SharedPreferences setting = context.getSharedPreferences("UtilsShard", 0);
        Boolean first = setting.getBoolean("show", true);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time2 = setting.getString("TIME", "");
        if (TextUtils.isEmpty(time2)){
            setting.edit().putString("TIME", sdf.format(new Date())).apply();
        }else {
            if (first)  {
                Date parse = null;
                try {
                    parse = sdf.parse(setting.getString("TIME", "2019-12-11 12:00:00"));
                    long time = new Date().getTime();
                    long time1 = parse.getTime();
                    long l = calDateDifferent(time1, time);
                    if (l>=2){
                        setting.edit().putBoolean("show", false).apply();
                        setting.edit().putString("TIME", sdf.format(new Date())).apply();
                        super.show();
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            } else {
                Date parse = null;
                try {
                    parse = sdf.parse(setting.getString("TIME", "2019-12-11 12:00:00"));
                    long time = new Date().getTime();
                    long time1 = parse.getTime();
                    long l = calDateDifferent(time1, time);
                    if (l>=7){
                        setting.edit().putString("TIME", sdf.format(new Date())).apply();
                        super.show();
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
//
            }
        }

    }



    /***
     * 计算两个时间差，返回的是的秒s
     *
     *
     *
     * @return long
     * @param dete1
     * @param date2
     * @return
     */
    public static long calDateDifferent(Long dete1, Long date2) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long diff = date2 - dete1;
        return diff / 1000 / 3600 /24;
    }

}